<template>
  <div class="dining-improvement-container">
    <el-card class="box-card">
      <template #header>
        <div class="card-header">
          <span>餐饮建议改进管理</span>
          <el-button type="primary" @click="handleAddImprovement">添加建议</el-button>
        </div>
      </template>

      <el-table :data="improvementList" style="width: 100%">
        <el-table-column prop="userName" label="姓名" width="120" />
        <el-table-column prop="category" label="类别" width="120" />
        <el-table-column prop="items" label="建议条目">
          <template #default="scope">
            <el-tag
              v-for="item in scope.row.items"
              :key="item"
              class="mx-1"
              closable
              @close="handleRemoveItem(scope.row, item)"
            >
              {{ item }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="200" fixed="right">
          <template #default="scope">
            <el-button
              size="small"
              type="primary"
              @click="handleEdit(scope.row)"
            >
              编辑
            </el-button>
            <el-button
              size="small"
              type="danger"
              @click="handleDelete(scope.row)"
            >
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!-- 添加/编辑对话框 -->
    <el-dialog
      v-model="dialogVisible"
      :title="dialogType === 'add' ? '添加建议' : '编辑建议'"
      width="50%"
    >
      <el-form :model="improvementForm" label-width="100px">
        <el-form-item label="姓名">
          <el-input v-model="improvementForm.userName" />
        </el-form-item>
        <el-form-item label="类别">
          <el-select v-model="improvementForm.category" placeholder="请选择类别">
            <el-option label="出炉产品" value="出炉产品" />
            <el-option label="口味调整" value="口味调整" />
            <el-option label="汤类" value="汤类" />
          </el-select>
        </el-form-item>
        <el-form-item label="建议条目">
          <el-select
            v-model="improvementForm.selectedItems"
            multiple
            filterable
            allow-create
            default-first-option
            placeholder="请选择或输入建议条目"
          >
            <el-option
              v-for="item in categoryOptions[improvementForm.category]"
              :key="item"
              :label="item"
              :value="item"
            />
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="handleSubmit">确定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { ref, reactive } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import axios from '@/utils/axios'

export default {
  name: 'DiningImprovement',
  setup() {
    const improvementList = ref([])
    const dialogVisible = ref(false)
    const dialogType = ref('add') // 'add' 或 'edit'
    const currentId = ref(null)

    const categoryOptions = reactive({
      '出炉产品': ['来饭', '干豆豆', '阿福', '胡椒撒椒', '香辣茄子'],
      '口味调整': ['同样子丁丁', '修改味', '三合一', '二合一'],
      '汤类': ['内头汤', '成汤', '生水丸子汤']
    })

    const improvementForm = reactive({
      userName: '',
      category: '',
      selectedItems: []
    })

    // 获取建议列表
    const fetchImprovementList = async () => {
      try {
        const response = await axios.get('/dining/improvement/list')
        improvementList.value = response.data
      } catch (error) {
        console.error('获取建议列表失败:', error)
        ElMessage.error('获取建议列表失败')
      }
    }

    // 添加建议
    const handleAddImprovement = () => {
      dialogType.value = 'add'
      improvementForm.userName = ''
      improvementForm.category = ''
      improvementForm.selectedItems = []
      dialogVisible.value = true
    }

    // 编辑建议
    const handleEdit = (row) => {
      dialogType.value = 'edit'
      currentId.value = row.id
      improvementForm.userName = row.userName
      improvementForm.category = row.category
      improvementForm.selectedItems = row.items
      dialogVisible.value = true
    }

    // 删除建议
    const handleDelete = (row) => {
      ElMessageBox.confirm('确定要删除这条建议吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        try {
          await axios.delete(`/dining/improvement/${row.id}`)
          ElMessage.success('删除成功')
          fetchImprovementList()
        } catch (error) {
          console.error('删除失败:', error)
          ElMessage.error('删除失败')
        }
      })
    }

    // 移除建议条目
    const handleRemoveItem = async (row, item) => {
      try {
        await axios.post(`/dining/improvement/${row.id}/removeItem`, { item })
        ElMessage.success('移除条目成功')
        fetchImprovementList()
      } catch (error) {
        console.error('移除条目失败:', error)
        ElMessage.error('移除条目失败')
      }
    }

    // 提交表单
    const handleSubmit = async () => {
      try {
        if (dialogType.value === 'add') {
          await axios.post('/dining/improvement/add', {
            userName: improvementForm.userName,
            category: improvementForm.category,
            items: improvementForm.selectedItems
          })
        } else {
          await axios.put(`/dining/improvement/${currentId.value}`, {
            userName: improvementForm.userName,
            category: improvementForm.category,
            items: improvementForm.selectedItems
          })
        }
        ElMessage.success(dialogType.value === 'add' ? '添加成功' : '更新成功')
        dialogVisible.value = false
        fetchImprovementList()
      } catch (error) {
        console.error('操作失败:', error)
        ElMessage.error('操作失败')
      }
    }

    // 初始化
    fetchImprovementList()

    return {
      improvementList,
      dialogVisible,
      dialogType,
      improvementForm,
      categoryOptions,
      handleAddImprovement,
      handleEdit,
      handleDelete,
      handleRemoveItem,
      handleSubmit
    }
  }
}
</script>

<style scoped>
.dining-improvement-container {
  padding: 20px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.el-tag {
  margin-right: 8px;
  margin-bottom: 8px;
}
</style> 